<?php
namespace Mytype\Controller;
use Common\Controller\AdminbaseController;
use Org\Util\DataTree;

/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/6/14
 * Time: 12:52
 */
class IndexController extends AdminbaseController
{
    public function index()
    {
        $tree = new DataTree();
        $data = $tree->tree(M('city')->select(),'name','id');
        $this->assign('data',$data);
        $this->display();
    }
    /*添加城市分类*/
    public function add()
    {
        if(IS_POST){
            $post  = i('post.');
            $data = [
              'name'=>$post['name'],
                'des'=>$post['des']
            ];
            if(trim(empty($post))){
                $this->error('请检查输入，描述或者城市名为空');
            }else{
                if(M('city')->add($data)){
                    $this->success('添加成功');
                }else{
                    $this->error('失败，未知错误');
                }
            }
        }else{
            $this->display();
        }
    }
    /*修改城市*/
    public function edit()
    {
        $id = i('get.id');
        $this->assign('vo',M('city')->where(['id'=>$id])->find());
        if(IS_POST){
            $post  = i('post.');
            $data = [
                'name'=>$post['name'],
                'des'=>$post['des']
            ];
            if(trim(empty($post))){
                $this->error('请检查输入，描述或者城市名为空');
            }else{
                if(M('city')->where(['id'=>$id])->save($data)){
                    $this->success('修改成功');
                }else{
                    $this->error('失败，未知错误');
                }
            }
        }else{
            $this->display();
        }
    }
    /*删除城市*/
    public function del()
    {
        $id = i('get.id');
        if(M('city')->where(['id'=>$id])->delete()){
            $this->success('删除成功');
        }else{
            $this->error('失败，未知错误');
        }
    }
    /*添加子类城市*/
    public function addson()
    {
        $id = i('get.id');
        $this->assign('vo',M('city')->where(['id'=>$id])->find());
        if(IS_POST){
            $post  = i('post.');
            $data = [
                'name'=>$post['name'],
                'des'=>$post['des'],
                'pid'=>$id
            ];
            if(trim(empty($post))){
                $this->error('请检查输入，描述或者城市名为空');
            }else{
                if(M('city')->add($data)){
                    $this->success('添加子类城市成功');
                }else{
                    $this->error('失败，未知错误');
                }
            }
        }else{
            $this->display();
        }
    }
}